package com.jueling.culture.util;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


/**
 * @author linp
 * @date 2019/7/28
 */
public class JsonUtil {
    public static ObjectMapper mapper = new ObjectMapper();

    /**
     * Object转JsonString
     *
     * @param bean
     * @return
     */
    public static String object2JsonString(Object bean) {
        try {
            return mapper.writeValueAsString(bean);
        } catch (JsonProcessingException e) {
            return bean == null ? null : bean.toString();
        }
    }

    /**
     * Object转JsonString  不包括 null 字段
     *
     * @param bean
     * @return
     */
    public static String object2JsonStringWithoutNull(Object bean) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            return mapper.writeValueAsString(bean);
        } catch (JsonProcessingException e) {
            return bean == null ? null : bean.toString();
        }
    }

    /**
     * Object转Map  不包括 null 字段
     *
     * @param bean
     * @return
     */
    public static Map<String, Object> object2map(Object bean) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            String jsonStr = mapper.writeValueAsString(bean);
            return jsonString2Map(jsonStr);
        } catch (JsonProcessingException e) {
            return null;
        }

    }


    /**
     * JsonString转Object
     *
     * @param str
     * @param valueType
     * @param <T>
     * @return
     */
    public static <T> T jsonString2Object(String str, Class<T> valueType) {
        try {
            T t = mapper.readValue(str, valueType);
            return t;
        } catch (IOException e) {
            return null;
        }

    }

    /**
     * json str 转成 map
     *
     * @param jsonStr
     * @return
     */
    public static Map<String, Object> jsonString2Map(String jsonStr) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            Map<String, Object> map = mapper.readValue(jsonStr, Map.class);
            return map;
        } catch (IOException e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }

    /**
     * map 转成 json str
     *
     * @param map
     * @return
     */
    public static String map2JsonString(Map<String, Object> map) {
        try {
            return mapper.writeValueAsString(map);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }




    /**
     * 使用 Map按key进行排序
     *
     * @param map
     * @return
     */
    public static Map<String, Object> sortMapByKey(Map<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        Map<String, Object> sortMap = new TreeMap<String, Object>(new MapKeyComparator());
        sortMap.putAll(map);
        return sortMap;
    }
}


/**
 * 比较器类
 */
class MapKeyComparator implements Comparator<String> {
    @Override
    public int compare(String str1, String str2) {
        return str1.compareTo(str2);
    }

}